package com.tsingteng.cosfun.ui.found;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.util.Pair;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.tsingteng.cosfun.MainActivity;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.cosfun.OpuslFragmentNew;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.NotchUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author zht
 */

public class ActivityVideoActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    String tag = ActivityVideoActivity.class.getSimpleName();
    ViewPager viewpager = null;
    List<Pair<String, Fragment>> mFragmentPair = null;
    int currentPosition = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (null != savedInstanceState) {
            savedInstanceState.remove(Config.FRAGMENTS_TAG);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found_video_at);
        NotchUtil.setWindowTrans(this, true, false);
        initView();
    }
    protected void initView() {
        List<OpusBean> coverImgUrl = (List<OpusBean>) getIntent().getSerializableExtra(Constant.COVERIMGURL);
        int mCurrentPosition = getIntent().getIntExtra(Constant.POSITION, 0);
        viewpager = findViewById(R.id.found_Video_vp);
        mFragmentPair = new ArrayList<Pair<String, Fragment>>();

//        Pair awkwardPair = new Pair("awkward", new AwkwardFragment());
        OpuslFragmentNew opuslFragmentNew=new OpuslFragmentNew();
        Pair opusPair = new Pair("opus", opuslFragmentNew);
        HomeMineFragment homeMineFragment = new HomeMineFragment();
        homeMineFragment.setBackLisenter(backLisenter);
        Pair personalPair = new Pair("personal", homeMineFragment);
//        mFragmentPair.add(awkwardPair);
        mFragmentPair.add(opusPair);
        mFragmentPair.add(personalPair);
        MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(), mFragmentPair);
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(currentPosition);
        viewpager.setOffscreenPageLimit(2);
        viewpager.addOnPageChangeListener(this);

    }
    @Override
    protected void onResume() {
        super.onResume();
        if (currentPosition == 0) {
            if (mFragmentPair.get(currentPosition).first.equals("foundVideo")) {
                ((FoundVideoPlayFragment) (mFragmentPair.get(currentPosition).second)).onPlay();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (currentPosition == 1) {
            if (mFragmentPair.get(currentPosition).first.equals("foundVideo")) {
                ((FoundVideoPlayFragment) (mFragmentPair.get(currentPosition).second)).onPause();
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
        switch (currentPosition) {
            case 0:
//                        if (mFragmentPair.get(0).first.equals("awkward")) {
//                            ((AwkwardFragment) (mFragmentPair.get(0).second)).getData();
//                            ((AwkwardFragment) (mFragmentPair.get(0).second)).playVideo(-1);
//                        }
                if (mFragmentPair.get(0).first.equals("opus")) {
                    ((OpuslFragmentNew) (mFragmentPair.get(0).second)).play();
                }
                break;
            case 1:
//                        if (mFragmentPair.get(0).first.equals("awkward")) {
//                        }
                if (mFragmentPair.get(0).first.equals("opus")) {
                    ((OpuslFragmentNew) (mFragmentPair.get(0).second)).onPause();
                }
                if (mFragmentPair.get(1).first.equals("personal")) {
                    ((HomeMineFragment) (mFragmentPair.get(1).second)).refreshData();
                    ((HomeMineFragment) (mFragmentPair.get(1).second)).goneButtomView(View.GONE);

                }
                break;
            case 2:
                if (mFragmentPair.get(1).first.equals("opus")) {
                    ((OpuslFragmentNew) (mFragmentPair.get(1).second)).onPause();
                }
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

        private List<Pair<String, Fragment>> mFragmentPair;

        public MyFragmentStatePagerAdapter(FragmentManager fm, List<Pair<String, Fragment>> mFragmentPair) {
            super(fm);
            this.mFragmentPair = mFragmentPair;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentPair.get(position).second;
        }

        /**
         * adapter.notifyDataSetChanged()的时候，ViewPager会去查询到底哪个位置有改变，
         * return PagerAdapter.POSITION_NONE; 就是告诉所有的View已经变化，从而达到刷新的目的
         *
         * @param object
         * @return
         */
        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
            //  return super.getItemPosition(object);
        }

        @Override
        public int getCount() {
            return mFragmentPair.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentPair.get(position).first;
        }
    }

    HomeMineFragment.BackLisenter backLisenter = new HomeMineFragment.BackLisenter() {
        @Override
        public void back() {
            viewpager.setCurrentItem(0);
        }
    };


}
